﻿// description  : 访问控制
// author       : tristan
// created      : 2011/2/16
// contact      : snaprack@gmail.com

namespace RBAC
{
    using System;
    using System.Linq;
    /// <summary>
    /// 访问控制接口
    /// </summary>
    public interface IAccessControl
    {
        /// <summary>
        /// 设置上下文
        /// </summary>
        /// <param name="user">用户名</param>
        /// <param name="resource">访问资源</param>
        void SetContext(string user, string resource);

        /// <summary>
        /// 是否允许访问
        /// </summary>
        bool Accessible { get; }
    }

    /// <summary>
    /// 一般的访问控制实现
    /// </summary>
    public class GeneralAccessControl : IAccessControl
    {
        /// <summary>
        /// 用户
        /// </summary>
        protected string User { get; set; }
        /// <summary>
        /// 访问目标
        /// </summary>
        protected string Resource { get; set; }

        #region IAccessControl 成员

        public virtual void SetContext(string user, string resource)
        {
            User = user;
            Resource = resource;
        }

        public virtual bool Accessible
        {
            get
            {
                Authorization auth = new Authorization(this.User);

                return auth.IsAuthorized(this.Resource);
            }
        }

        #endregion
    }

}